#pragma once

#include<iostream>
#include<string>
#include<assert.h>

using namespace std;

#pragma once

#include<iostream>
#include<string>
#include<assert.h>

using namespace std;

namespace lyz
{
	class string
	{
	public:

		typedef char* iterator;
		typedef const char* const_iterator;
		iterator begin()
		{
			return _str;
		}

		const_iterator begin()const
		{
			return _str;
		}

		iterator end()
		{
			return _str + _size;
		}

		const_iterator end()const
		{
			return _str + _size;;
		}

		string(const char* s = "")
		{
			_size = strlen(s);
			_capacity = _size;
			_str = new char[_capacity + 1];
			strcpy(_str, s);
		}

		string(const string& str)
		{
			_str = new char[str._capacity + 1];
			_size = str._size;
			_capacity = str._capacity;
			strcpy(_str, str._str);
		}

		string& operator= (const string& str)
		{
			if (this != &str)
			{
				char* tmp = new char[str._capacity + 1];
				strcpy(tmp, str._str);
				delete[]_str;
				_str = tmp;
				_size = str._size;
				_capacity = str._capacity;
			}
			return *this;
		}

		~string()
		{
			delete[]_str;
			_str = nullptr;
			_capacity = _size = 0;
		}
		char* c_str()
		{
			return _str;
		}

		size_t size()
		{
			return _size;
		}

		void reserve(int n)
		{
			if (n >= _capacity)
			{
				char* tmp = new char[n + 1];
				strcpy(tmp, _str);
				delete[] _str;
				_str = tmp;
				_capacity = n;
			}
		}



		char& operator[](int pos)
		{
			return _str[pos];
		}
		const char& operator[](int pos) const
		{
			return _str[pos];
		}


		void push_back(char ch)
		{
			if (_size == _capacity)
			{
				size_t newcapacity = _capacity == 0 ? 4 : _capacity * 2;
				reserve(newcapacity);
			}

			_str[_size] = ch;
			_size++;
			_str[_size] = '\0';
		}

		void append(const char* str)
		{
			size_t len = strlen(str);
			if (_size + len >= _capacity)
			{
				reserve(_size + len);
			}

			strcpy(_str + _size, str);
			_size += len;
		}

		string& operator+= (char ch)
		{
			push_back(ch);
			return *this;
		}

		string& operator+=(const char* str)
		{
			append(str);
			return*this;
		}

		void insert(size_t pos, char ch)
		{
			if (_size == _capacity)
			{
				int newcapacity = _capacity == 0 ?  4 : _capacity * 2;
				reserve(newcapacity);
				_capacity = newcapacity;
			}

			int end = _size;
			while (end >= (int)pos)
			{
				_str[end + 1] = _str[end];
				--end;
			}

			_str[pos] = ch;
			_size++;
		}

		void insert(size_t pos, const char* str)
		{
			int len = strlen(str);
			if (_size + len >= _capacity)
			{
				reserve(_size + len);
				_capacity = _size + len;
			}
			int end = _size;
			while (end >= (int)pos)
			{
				_str[end + len] = _str[end];
				--end;
			}
			strncpy(_str + pos, str, len);
			_size += len;

		}

		void erase(size_t pos, size_t len = npos)
		{
			assert(pos < _size);

			if (len == npos || pos + len >= _size)
			{
				_str[pos] = '\0';
				_size = pos;
			}
			else
			{
				strcpy(_str + pos, _str + pos + len);
				_size -= len;
			}
		}

		void swap(string& s)
		{
			std::swap(_str, s._str);
			std::swap(_size, s._size);
			std::swap(_capacity, s._capacity);
		}

		size_t find(char ch, size_t pos = 0)
		{
			assert(pos <= _size);

			for (size_t i = pos; i < _size; i++)
			{
				if (_str[i] == ch)
				{
					return i;
				}
			}
				return npos;
			
		}

		size_t find(const char* str, size_t pos = 0)
		{
			assert(pos <= _size);

			const char* tmp = strstr(_str+pos, str);
			if (tmp != NULL)
			{
				return tmp - _str;
			}
			else
			{
				return npos;
			}
		}

		string substr(size_t pos = 0, size_t len = npos)
		{
			assert(pos <= _size);

			int end = pos + len;

			if (len == npos || pos + len >= _size)
			{
				end = _size;
			}

			string ret;
			ret.reserve(end - pos);

			for (int i = pos; i < end; i++)
			{
				ret += _str[i];

			}

			/*while (end >= pos)
			{
				ret += _str[pos];
				pos++;
			}*/

			return ret;
		}


	private:
		char* _str;
		size_t _size;
		size_t _capacity;
		const static size_t npos = -1;
	};

	void testString()
	{
		/*string s1("hello world");
		cout << s1.c_str() << endl;

		string s2;
		cout << s2.c_str() << endl;

		for (size_t i = 0; i < s1.size(); i++)
		{
			s1[i]++;
		}
		cout << s1.c_str() << endl;

		string::iterator it = s1.begin();

		while (it != s1.end())
		{
			cout << *it << " ";
			it++;
		}
		cout << endl;
		for (auto ch : s1)
		{
			cout << ch << " ";
		}*/

		//const string s1("hello world");
		//cout << s1[0];
		////s1[0]++;

		//string::const_iterator it = s1.begin();
		//while (it != s1.end())
		//{
		//	cout << *it << " ";
		//	it++;
		//}

		string s1("hello world");
		cout << s1.c_str() << endl;

		/*s1.push_back('x');
		cout << s1.c_str() << endl;

		s1.append("lyz");
		cout << s1.c_str() << endl;
		s1.insert(0, 'a');
		cout << s1.c_str() << endl;

		s1.insert(0, "XXXX");
		cout << s1.c_str() << endl;*/

		/*s1.erase(5);
		cout << s1.c_str() << endl;*/

		/*cout << s1.find('e')<<endl;
		cout << s1.find("world");*/


		string str("https://legacy.cplusplus.com/reference/string/string/substr/");
		string sub1, sub2, sub3;
		size_t pos1 = str.find(':');
		sub1 = str.substr(0, pos1 - 0);
		cout << sub1.c_str() << endl;

		size_t pos2 = str.find('/', pos1 + 3);
		sub2 = str.substr(pos1 + 3, pos2 - (pos1 + 3));
		cout << sub2.c_str() << endl;

		sub3 = str.substr(pos2 + 1);
		cout << sub3.c_str() << endl;

	}
};
